--[[
Author       : Bruce
Date         : 2024-12-22 12:05:48
LastEditors  : Bruce
LastEditTime : 2024-12-22 12:06:57
FilePath     : /modules/display-control/brightness.lua
Description  :
Copyright 2024 Bruce, All Rights Reserved.
2024-12-22 12:05:48
--]]
local M = {}
local screen = require("hs.screen")
local task = require("hs.task")

-- 亮度调节步长
local STEP = 10

-- m1ddc 命令的完整路径
local M1DDC_CMD = "/opt/homebrew/bin/m1ddc"

-- 亮度指示器
local brightnessIndicator = nil

-- 当前亮度的缓存
local currentBrightness = 50  -- 默认值

-- 创建或更新亮度指示器
local function showBrightnessIndicator(brightness)
    -- 如果指示器存在，先删除
    if brightnessIndicator then
        brightnessIndicator:delete()
    end

    -- 获取主屏幕尺寸
    local screen = hs.screen.mainScreen()
    local frame = screen:frame()

    -- 创建指示器窗口
    local width = 200
    local height = 40
    local x = frame.x + (frame.w - width) / 2
    local y = frame.y + frame.h - height - 100  -- 距离底部100像素

    -- 创建指示器
    brightnessIndicator = hs.drawing.text(hs.geometry.rect(x, y, width, height), "亮度: " .. brightness .. "%")
    brightnessIndicator:setTextStyle({
        textFont = "SF Pro",
        textSize = 24,
        textColor = {hex = "#FFFFFF"},
        textAlignment = "center",
        paragraphStyle = {
            alignment = "center"
        }
    })
    brightnessIndicator:setLevel(hs.drawing.windowLevels.overlay)
    brightnessIndicator:show()

    -- 2秒后自动隐藏
    hs.timer.doAfter(2, function()
        if brightnessIndicator then
            brightnessIndicator:delete()
            brightnessIndicator = nil
        end
    end)
end

-- 执行命令行并获取输出
local function executeCommand(command)
    print("Executing command:", command)
    local output = ""
    local task = task.new("/bin/bash", nil, function(exitCode, stdOut, stdErr)
        if exitCode == 0 then
            output = stdOut
        else
            print("Error:", stdErr)
        end
        return true
    end, {"-c", command})

    task:start()
    task:waitUntilExit()
    return output:gsub("\n$", "")
end

-- 获取当前亮度（从缓存）
local function getCurrentBrightness()
    return currentBrightness
end

-- 设置亮度
local function setBrightness(value)
    -- 确保值在有效范围内
    value = math.max(0, math.min(100, value))

    local cmd = string.format("%s set luminance %d", M1DDC_CMD, value)
    local output = executeCommand(cmd)
    print("Set brightness output:", output)

    -- 更新缓存的亮度值
    currentBrightness = value

    -- 显示亮度指示器
    showBrightnessIndicator(value)

    return value
end

function M.increase()
    local current = getCurrentBrightness()
    setBrightness(current + STEP)
end

function M.decrease()
    local current = getCurrentBrightness()
    setBrightness(current - STEP)
end

-- 添加调试函数
function M.debug()
    -- 列出所有显示器
    print("\nDisplays:")
    print(executeCommand(M1DDC_CMD .. " display list detailed"))

    -- 获取当前亮度
    print("\nTrying to get current brightness:")
    local current = getCurrentBrightness()
    print("Current brightness:", current)

    -- 尝试设置亮度为50
    print("\nTrying to set brightness to 50:")
    setBrightness(50)
end

return M